package com.cti.ftpservice.service.impl;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cti.ftpservice.ftp.FileUtils;
import com.cti.ftpservice.service.FtpOnixService;
import com.cti.ftpservice.utils.FtpConfigInfo;
import com.cti.ftpservice.vo.FTPOnixXmlAnalysisTaskInfoVo;
import com.cti.ftpservice.vo.FtpDownLoadBaseServiceInfoVo;
import com.cti.ftpservice.vo.FtpFileInfoVo;
import com.cti.ftpservice.vo.FtpOnixXmlInfoVo;
import com.ctibook.booksystem.ws.FtpFileDownloadViewVo;
import com.ctibook.booksystem.ws.FtpFileInfoViewVo;
import com.ctibook.booksystem.ws.FtpServiceViewVo;
import com.ctibook.booksystem.ws.FtpServiceWS;


@Service("ftpOnixService")
public class FtpOnixServiceImpl implements FtpOnixService {

	private static Log logger = LogFactory.getLog(FtpServiceImpl.class);
	@Autowired
	private FtpConfigInfo ftpConfigInfo;
	
	@Autowired
	private FtpServiceWS  ftpServiceWS;
	
	public void downloadFilesFromFtpServices() {
		String savePath=ftpConfigInfo.getOnixFileSavePathRoot();
		try {
			List<FtpServiceViewVo> ftpServiceViewVos=ftpServiceWS.getFtpServiceViewVosByType(FtpDownLoadBaseServiceInfoVo.FtpServiceType_OnixDownloadFile, 1);
			for (FtpServiceViewVo ftpServiceViewVo : ftpServiceViewVos) {
		   		 FtpOnixXmlInfoVo ftpDownLoadParams=new FtpOnixXmlInfoVo();
		   		 ftpDownLoadParams.setId(ftpServiceViewVo.getId());
		   		 ftpDownLoadParams.setFtpServiceType(FtpDownLoadBaseServiceInfoVo.FtpServiceType_OnixDownloadFile);
		       	 ftpDownLoadParams.setFtpport(ftpServiceViewVo.getPort());
		       	 ftpDownLoadParams.setFtpURL(ftpServiceViewVo.getUrl());
		       	 ftpDownLoadParams.setUsername(ftpServiceViewVo.getUserName());
		       	 ftpDownLoadParams.setPwd(ftpServiceViewVo.getPassWrod());
		       	 String ftpServiceFileSavePath=savePath+ftpServiceViewVo.getFtpServiceName()+"\\";
		       	 ftpDownLoadParams.setSavePath(ftpServiceFileSavePath);
		       	
		       	 List<String> files=ftpServiceViewVo.getDownloadFilePaths();
		       	 //TODO:获取已经下载的文件信息	    	
		    	 List<FtpFileDownloadViewVo> hasDownLoadFiles=ftpServiceWS.getFtpDownloadFileViewVoByFtpServiceIdAndDownloadPath(ftpServiceViewVo.getId(), files);
		       	 //获取远程Ftp服务器中文件路径下待下载的文件信息
		    	 List<FtpFileInfoVo> fileResultPaths = FileUtils.getXmlFilesFromFtpServiceFile(files,ftpDownLoadParams.getFtpURL(), 21 ,ftpDownLoadParams.getUsername(), ftpDownLoadParams.getPwd(), ftpServiceFileSavePath);
		    	 Queue<FtpFileInfoVo> downloadFiles= new LinkedList<FtpFileInfoVo>();
		   		 for (FtpFileInfoVo file : fileResultPaths) {
		   			 boolean flag=true;
		   			 if(hasDownLoadFiles!=null&&hasDownLoadFiles.size()>0){
		   				 for (FtpFileDownloadViewVo hasDownLoadFile : hasDownLoadFiles) {
									if(hasDownLoadFile.getDownloadFtpFilePath().equals(file.getFtpServicePath())
										&& hasDownLoadFile.getDownloadFtpFileSize()==file.getFileSize())
										flag=false;
								}
		   			 }
		   			if(flag)
		   			 downloadFiles.offer(file);
		   		 }
		   		 if(downloadFiles==null||downloadFiles.size()==0)
		   			 return;
		   		 if(downloadFiles.size()>=5)
		   			 ftpDownLoadParams.setThreadCount(5);
		   		 else
		   			 ftpDownLoadParams.setThreadCount(downloadFiles.size());
		   		 ftpDownLoadParams.setDownFileInfos(downloadFiles);
		   		 ftpDownLoadParams.setBookTotalCount(fileResultPaths.size());
		   		 ftpDownLoadParams.startDownLoadFiles();
			}
		} catch (Exception e) {
			logger.error(e.getCause().toString()+":"+e.getMessage());
		}
	}

	@Override
	public List<FtpFileInfoViewVo> getOnixUnDealXmlFiles() {
		List<FtpFileInfoViewVo> undealFileVos=ftpServiceWS.getOnixUnDealXmlFiles();
		return undealFileVos;
	}
	
	public void saveBookinfosFromOnixXmlFiles(){
		List<FtpFileInfoViewVo> viewVos=ftpServiceWS.getOnixUnDealXmlFiles();
		if(viewVos==null||viewVos.size()==0)return;
		logger.info("共有未解析文件："+viewVos.size()+"个;");
		try {
			FTPOnixXmlAnalysisTaskInfoVo taskInfo=new FTPOnixXmlAnalysisTaskInfoVo();
			taskInfo.setThreadCount(5);
			for (FtpFileInfoViewVo ftpFileInfoViewVo : viewVos) {
				taskInfo.addObjectItem(ftpFileInfoViewVo);
			}
			taskInfo.setTotalCount(viewVos.size());
			taskInfo.startWork();
		} catch (Exception e) {
			logger.error(e.getCause().toString()+e.getMessage());
		}
	}
}
